const path = require("path");
const Router = require("koa-router");
const { koaBody } = require("koa-body");

const {
  add,
  findAll,
  delList,
  findOne,
  updateList,
} = require("../../controllers/admin/product.controller");
const { auth } = require("../../middleware/admin/auth.middleware");

const router = new Router({ prefix: "/adminapi" });

router.post(
  "/product/add",
  auth,
  koaBody({
    multipart: true,
    formidable: {
      //配置选项option，不推荐使用相对路径
      //在option里的相对路径，不是相对的当前文件，相对于process.cwd
      uploadDir: path.join(__dirname, "../../public/productuploads"),
      keepExtensions: true,
    },
  }),
  add
);

router.get("/product/list", findAll);
router.get("/product/list/:id", findOne);
router.post(
  "/product/list",
  auth,
  koaBody({
    multipart: true,
    formidable: {
      //配置选项option，不推荐使用相对路径
      //在option里的相对路径，不是相对的当前文件，相对于process.cwd
      uploadDir: path.join(__dirname, "../../public/productuploads"),
      keepExtensions: true,
    },
  }),
  updateList
);
router.delete("/product/list/:id", delList);

module.exports = router;
